package com.prokarma.pkmst.service.Impl;

import com.prokarma.pkmst.security.Constants;
import com.prokarma.pkmst.security.Signer;
import com.prokarma.pkmst.service.RsaSecurityService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.TreeMap;

@Service
public class RsaSecurityServiceImpl implements RsaSecurityService {

    @Override
    public boolean checkRSA(String timestamp, String nonce, String companyNo, String signature, String payload) {
        Map<String, String> sortedMap = new TreeMap<>();
        sortedMap.put("timestamp", timestamp);
        sortedMap.put("nonce", nonce);
        sortedMap.put("companyNo", companyNo);
        sortedMap.put("data", payload);
        String content = Signer.getSignCheckContent(sortedMap);
        try {
            return Signer.verify(content,signature, Constants.PUBLICKEYPEM);
        }catch (Exception ex){
            return false;
        }
    }
}
